﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;
using System.IO;
using System.Web;

namespace YouHP.Pages.Group
{
    public class ReplyAdd : YouHP.Pages.Group.GroupBasePage
    {
        protected TopicInfo TopicData { get; set; }
        protected ReplyInfo ReplyData { get; set; }
        protected int replyCount;
        protected int tid;
        protected int rid;
        public int MaxFileSize = 1000;
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

    
            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected new void HandleCommon()
        {
            this.SetTitle("发表回复");
            //判断是否登录
            if (UserData.Id <= 0)
            {
                this.AddErrLine("您还没有登录，请点击登录！");
                this.MsgboxUrl = this.GetUrl("/User/Login.aspx");
                return;
            }

            bool succ = false;
   

                if (GroupUserData.Role == Constants.RoleCreator
                    || GroupUserData.Role == Constants.RoleAdmin
                    || GroupUserData.Role == Constants.RoleMember
                    )
                {
                    succ = true;
                }
                else
                {
                    this.AddErrLine("只有该圈子的成员才能发表回复,请先点击加入");
                    this.SetUrl("/Group/Join.aspx?gid="+GroupData.Id);
                    return;
                }

            if (succ == false) return;

            tid = RequestTools.GetQueryInt("tid", 0);

            if(tid <= 0)
            {
                this.AddErrLine("无效的参数tid");
                return;
            }
            TopicData = TopicBLL.Instance.GetTopicInfo(tid);
            if (TopicData.Id <= 0)
            {
                this.AddErrLine("帖子不存在或已被删除");
                return;
            }

            rid = RequestTools.GetQueryInt("rid", 0);
            if (rid > 0)
            {
            
                ReplyData = ReplyBLL.Instance.GetReplyInfo(rid);
            }
            else
            {
                ReplyData = new ReplyInfo();//表明是对主题的回复
            }

        }

        protected void HandlePost()
        {
            ReplyInfo info = ValidataForm();

            if (info == null) return;           

            string str = RequestTools.GetFormString("file");
            string attachedFileAddressName = Utils.SaveRequestTopicFile(UserData.Id, MaxFileSize);
            if (attachedFileAddressName.Equals(""))
            {
                AddErrLine("上传文件失败");
                return;

            }
            info.ReplyFile = attachedFileAddressName;
            info.ReplyFileName = Path.GetFileName(HttpContext.Current.Request.Files[0].FileName);
            bool succ=ReplyBLL.Instance.AddReply(info);
            if (succ)
            {

                string nexturl = "/Group/Topic.aspx?gid=" + GroupData.Id + "&tid="+TopicData.Id;
                this.Redirect(nexturl);

            }
            else
            {
                AddErrLine("发表回复失败");
            }
        }

        protected void HandleGet()
        {
            //this.AddScriptSrc(TinyMCE.Path);
            //this.AddScript(TinyMCE.InitAdvance("rbody"));
        }

        ReplyInfo ValidataForm()
        {
            ReplyInfo info = new ReplyInfo();
            info.Body = RequestTools.GetFormString("rbody").Trim();
            if (info.Body.Length == 0)
            {
                this.AddErrLine("回复长度不能为空！");
                return null;
            }
            else if (info.Body.Length > Config.ReplyMaxSize)
            {
                this.AddErrLine("回复长度不能大于 " + Config.ReplyMaxSize + "字符！");
                return null;
            }
            info.TopicId = TopicData.Id;
            info.PostTime = DateTime.Now;
            info.PostUserId = UserData.Id;
            info.PostUserName = UserData.UserName;
            info.UpdateTime = DateTime.Now;
            info.ReplyAvatar = GroupUserData.UserAvatar;
            info.GroupId = GroupData.Id;
            info.FloorNo = TopicData.ReplyCount + 1;
            info.ReplyFloorNo = ReplyData.FloorNo;
            return info;
        }
    }
}